package com.runjian.rundo.iot.agent.business.publisher.impl;

import cn.hutool.core.util.ObjUtil;
import org.springframework.stereotype.Component;

import com.runjian.rundo.iot.agent.business.publisher.DeviceMessageProcessor;
import com.runjian.rundo.iot.agent.business.publisher.MessagePublisher;
import com.runjian.rundo.iot.agent.common.enums.DeviceMessageType;
import com.runjian.rundo.iot.agent.common.message.DecodedMessage;
import com.runjian.rundo.iot.agent.common.message.DeviceMessage;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 设备属性消息处理器
 *
 * @author LiGang
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DevicePropertiesMessageProcessor implements DeviceMessageProcessor<DeviceMessage> {

    private final MessagePublisher mqMessagePublisher;

    @Override
    public void process(DeviceMessage deviceMessage) {
        for (DecodedMessage decodedMessage : deviceMessage.getDecodedMessages()) {
            if(ObjUtil.isNotEmpty(decodedMessage.getPayload())){
                mqMessagePublisher.sendDeviceMessage(decodedMessage);
            }else{
                if(log.isDebugEnabled()) {
                    log.debug("设备属性消息为空,设备Id:{},网关Id:{},解析数据:{}", decodedMessage.getDeviceId(), decodedMessage.getGatewayId(),decodedMessage);
                }
            }
        }
    }

    @Override
    public DeviceMessageType getType() {
        return DeviceMessageType.PROPERTIES;
    }
}
